$(document).ready(function () {
  initSize()
  Judgeidentity()
  layui.use(function () {
    var form = layui.form;

    // 保存密码
    form.on('submit(password-form)', function (data) {
      var params = {
        id: JSON.parse(localStorage.getItem('userInfo')).userId,
        passwordOne: data.field.passwordOne,
        passwordTwo: data.field.passwordTwo,
      }
      AjaxRequest('/user/password/update', callBack, params, 'post')

      function callBack(result) {
        if (result.status == 200) {
          layer.closeAll()
          layer.msg('保存成功')
          layer.alert('请重新登录', {
            time: 5 * 1000,
            success: function (layero, index) {
              var timeNum = this.time / 1000,
                setText = function (start) {
                  layer.title('<span class="layui-font-red">' + (start ? timeNum : --timeNum) + '</span> 秒后跳转到登录界面', index);
                };
              setText(!0);
              this.timer = setInterval(setText, 1000);
              if (timeNum <= 0) clearInterval(this.timer);
            },
            end: function () {
              clearInterval(this.timer);
              localStorage.setItem('sessionId', "");
              localStorage.setItem('userInfo', "");
              window.location.href = "../html/login.html";
            }
          });
        } else {
          layer.msg(result.result)
        }
      }

      return false;
    })

  })
})

function initSize() {
  //适应不同分辨率
  let fontSize;
  resize();
  window.onresize = resize;

  function resize() {

    let winW = window.innerWidth;

    if (winW < 1366) {
      winW = 1366;
    }
    fontSize = winW / 1920 * 100
    document.getElementsByTagName('html')[0].style.cssText = "font-size:" + fontSize + "px";
  }
}

function Judgeidentity() {
  var userInfo = JSON.parse(localStorage.getItem('userInfo'));
  if (userInfo == null || userInfo == "") {
    window.location.href = "login.html";
  } else {
    var authId = userInfo.authId //1-管理员 2-普通用户
    if (authId == '2') {
      $('.left-btns-item')[2].style.display = 'none'
      $('.left-btns-item')[3].style.display = 'none'
      $('.left-btns-item')[4].style.display = 'none'
    } else {
      $('.left-btns-item')[2].style.display = 'block'
      $('.left-btns-item')[3].style.display = 'block'
      $('.left-btns-item')[4].style.display = 'block'
    }
  }
}

function ajaxPrefix() {
  return '/rest/dtchat'
}

function AjaxRequest(url, callBack, parms, type) {
  $.ajax({
    url: '/rest/dtchat' + url,
    type: type ? type : 'get',
    data: parms ? JSON.stringify(parms) : '',
    headers: {
      SessionId: JSON.parse(localStorage.getItem('sessionId'))
    },
    contentType: "application/json",
    success: function (result) {

      callBack(result);

    }
  });
}

function logout() {
  layer.confirm('确定退出系统？', {
    btn: ['确定', '取消'] //按钮
  }, function () {
    localStorage.setItem('sessionId', "");
    localStorage.setItem('userInfo', "");
    window.location.href = "../html/login.html";
  });
}

function showEditPasswordWindow() {
  layer.open({
    type: 1,
    shadeClose: false,
    move: false,
    closeBtn: 2,
    skin: 'default-layer',
    area: ['auto', 'auto'],
    title: '修改密码',
    content: `<div id="password-from" class="layui-form" style="padding: .2rem 0.6rem;color:#E4EFFF">
                <div class="layui-form-item">
                  <div class="layui-inline">
                    <div>请输入密码：</div>
                    <div style="margin-top: .1rem;">
                      <input type="text" name="passwordOne" lay-verify="required" class="layui-input">
                    </div>
                  </div>
                </div>
                <div class="layui-form-item">
                  <div class="layui-inline">
                    <div>再次输入密码：</div>
                    <div style="margin-top: .1rem;">
                      <input type="text" name="passwordTwo" lay-verify="required" class="layui-input">
                    </div>
                  </div>
                </div>
                <div class="layui-form-item" style="text-align:center">
                  <button type="submit" class="layui-btn layui-btn-lg layui-bg-blue" lay-submit lay-filter="password-form" style="border-radius:.08rem">保  存</button>
                </div>
              </div>`,
    success: function (layero, index) {}
  });
}